package com.nowcoder.community.controller;/**
 * 功能描述
 *
 * @author 21775
 * @date 2022/09/18  23:28
 */

import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.event.ProducerEvent;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.utils.CommunityConstUtil;
import com.nowcoder.community.utils.HostHolder;
import com.nowcoder.community.utils.RedisKeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;

/**
 *@ClassName CommentController
 *@Description TODO 评论控制层
 *@Author 21775
 *@Date 2022/9/18 23:28
 *@Version 1.0
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private HostHolder hostHolder;

    @Resource
    private ProducerEvent producerEvent;

    @Resource
    private DiscussPostService discussPostService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping("/add/{discussPostId}")
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        //触发评论事件
        Event event=new Event()
                .setTopic(CommunityConstUtil.TOPIC_COMMENT)
                .setUserId(hostHolder.getUser().getId())
                .setEntityId(comment.getEntityId())
                .setEntityType(comment.getEntityType())
                .setData("postId",discussPostId);
        if(comment.getEntityType()==CommunityConstUtil.ENTITY_TYPE_POST){
            DiscussPost discussPost=discussPostService.findDiscussPostById(comment.getEntityId());
            event.setEntityUserId(discussPost.getUserId());
        }else if(comment.getEntityType()==CommunityConstUtil.ENTITY_TYPE_COMMENT){
            Comment target=commentService.selectCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        producerEvent.fireEvent(event);

        if (comment.getEntityType() == CommunityConstUtil.ENTITY_TYPE_POST) {
            // 触发发帖事件
            event = new Event()
                    .setTopic(CommunityConstUtil.TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(CommunityConstUtil.ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            producerEvent.fireEvent(event);

            // 计算帖子分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussPostId);
        }

        return "redirect:/discuss/detail/" + discussPostId;
    }
}
